<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id="div1">
        <input type="checkbox" name="a" id="" value="banana">banana
        <input type="checkbox" name="a" id="" value="apple">apple
        <input type="checkbox" name="a" id="" value="orange">orange
    </div>
    <p></p>
    <script type="text/javascript">
    //1.使用onchange事件动态获取复选框的值
        var input = document.getElementsByTagName('input');
        var len = input.length;
        var p = document.querySelector('p')
        var div = document.getElementById('div1')
        console.log(div1);
        div1.onchange =function(){
            var str = '';
            for (let i = 0; i <len; i++) {
                if (input[i].checked) {
                    str = str+input[i].value+' ';
                }
               
             }
             p.innerText = str;
        }
    
    //2.
    </script>
   
   
</body>
</html>